package rensoft.android.view;

import rensoft.android.HasActivity;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

public class ViewBase implements HasView {

  private View view;

  public ViewBase() {}

  public ViewBase(HasActivity hasActivity, int rowLayoutId) {
    setView(hasActivity.activity().getLayoutInflater().inflate(rowLayoutId, null));
  }

  public ViewBase(View view) {
    this.view = view;
  }

  public View asView() {
    return view;
  }

  protected View findViewById(int id) {
    return view.findViewById(id);
  }

  protected Button getButton(int id) {
    return (Button) findViewById(id);
  }

  protected Context getContext() {
    return view.getContext();
  }

  protected EditText getEditText(int id) {
    return (EditText) findViewById(id);
  }

  protected ImageView getImageView(int id) {
    return (ImageView) findViewById(id);
  }

  protected ListView getListView(int id) {
    return (ListView) findViewById(id);
  }

  protected RadioButton getRadioButton(int id) {
    return (RadioButton) findViewById(id);
  }

  protected RadioGroup getRadioGroup(int id) {
    return (RadioGroup) findViewById(id);
  }

  protected ScrollView getScrollView(int id) {
    return (ScrollView) findViewById(id);
  }

  protected String getString(int id) {
    return getContext().getResources().getString(id);
  }

  protected TextView getTextView(int id) {
    return (TextView) findViewById(id);
  }

  protected void setView(View view) {
    this.view = view;
  }

  protected void setVisible(int viewId, boolean visible) {
    if (visible)
      findViewById(viewId).setVisibility(View.VISIBLE);
    else findViewById(viewId).setVisibility(View.GONE);
  }
}
